<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>广告添加</legend>
            <div>
                <form class="layui-form layui-form-pane" action="" lay-filter="adadd">

                    <div class="layui-form-item">
                        <label class="layui-form-label">广告备注</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入广告备注"
                                class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">绑定栏目</label>
                            <div class="layui-input-inline">
                                <select name="bindColumn" lay-filter="aihao" id="bindColumn">
                                    <option value="all">全局</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">广告代码</label>
                        <div class="layui-input-block">
                            <textarea id="code" placeholder="请输入广告代码,支持HTML,CSS,JS" name="code"
                                class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">固定排序</label>
                        <div class="layui-input-inline">
                            <input type="text" name="sort" lay-verify="title" autocomplete="off" placeholder="数字越大越靠前"
                                class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">固定广告</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="bind" lay-skin="switch" lay-text="ON|OFF">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">启用广告</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="close" lay-skin="switch" lay-text="ON|OFF">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <button type="button" class="layui-btn" lay-submit="" lay-filter="adadd">添加</button>
                    </div>
                </form>
            </div>
        </fieldset>
    </div>


</div>


<script>
    layui.use(['form', 'laytpl', 'lycms'], function () {
        var form = layui.form
            , layer = layui.layer,
            laytpl = layui.laytpl,
            lycms = layui.lycms;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //监听提交
        form.on('submit(adadd)', function (data) {
            $.ajax({
                url: "/ask/action/system/ad/add",
                type: "post",
                timeout: 10000,
                dataType: "json",
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                data: data.field,
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, { time: 1500 }, function () {
                            // window.location = '/admin/index#/bannerlist';
                        });
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (XMLResponse) {
                    //异常捕获
                    layer.msg(XMLResponse.responseText);
                }
            });
            return false;
        });

        // 页面初始化
        $(document).ready(function () {
            //获取栏目信息
            $.post("/v1/admin/data/platelist", function (data, status) {
                data.data.forEach(element => {
                    var option = '<option value="' + element.alias + '">' + element.title + '</option>';
                    $("#bindColumn").append(option);
                });
                form.render();
            });

        });

    });
</script>